﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LabMS.BLL;
using LabMS.Model;
using System.Collections.Generic;
using ExtendBLL;

namespace LabMS.Web.Equipment
{
    public partial class Dictionary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LabMS.Model.DictionaryClass mDClass = new LabMS.Model.DictionaryClass();
            if (!Page.IsPostBack)
            {
                string strDClass;
                if (Request.QueryString["Type"] == null)
                {
                    Common.JShelper.JSAlert(Page, "err", "参数错误!");
                    return;
                }

                strDClass = Request.QueryString["Type"].ToString();
                lbType.Text = strDClass;
                mDClass = checkQueryString(strDClass);
                if (mDClass == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "字典类别不存在!");
                    return;
                }
                lbPageName.Text = mDClass.Name;

                
                bind(strDClass);

                
            }
            this.Page.Title = lbPageName.Text;
        }

        LabMS.Model.DictionaryClass checkQueryString(string strDclass)
        {
            LabMS.BLL.DictionaryClass dicclass = new LabMS.BLL.DictionaryClass();
            string strWhere = " Code = '" + strDclass + "' ";
            List<LabMS.Model.DictionaryClass> listDClass = dicclass.GetModelList(strWhere);
            if (listDClass.Count <= 0)
            {
                return null;
            }
            return listDClass[0];
        }

        public void bind(string strCode)
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass(strCode);
            GridViewDic.DataSource = listCClass;
            GridViewDic.DataBind();            
        }

        protected void GridViewDic_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewDic.PageIndex = e.NewPageIndex;
            if (tbKeyWord.Text.Trim().Equals(""))
            {
                bind(lbType.Text);
            }
            else
            {
                string strwhere = " like ";
                string strcondition = ddlSType.SelectedValue;
                if (strcondition == "0")
                {
                    strwhere = " Code " + strwhere;
                }
                else
                {
                    strwhere = " Name " + strwhere;
                }
                strwhere = strwhere + " '%" + tbKeyWord.Text.Trim() + "%' ";

                LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();
                strwhere += " and DClass = '" + lbType.Text + "' " + " order by ID desc ";
                List<LabMS.Model.Dictionary> listDic = dic.GetModelList(strwhere);

                GridViewDic.DataSource = listDic;
                GridViewDic.DataBind();
            }
        }

        protected void GridViewDic_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");

                e.Row.Attributes.Add("OnDblClick", "DbClickEvent('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "')");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strwhere = " like ";
            string strcondition = ddlSType.SelectedValue;
            if (strcondition == "0")
            {
                strwhere = " Code " + strwhere;
            }
            else
            {
                strwhere = " Name " + strwhere;
            }
            strwhere = strwhere + " '%" + tbKeyWord.Text.Trim() + "%' ";

            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();
            strwhere += " and DClass = '" + lbType.Text + "' " + " order by ID desc ";
            List<LabMS.Model.Dictionary> listDic = dic.GetModelList(strwhere);

            GridViewDic.DataSource = listDic;
            GridViewDic.DataBind();
        }
    }
}
